<?php

namespace app\common\model\xiluhouse;

use think\Model;

/**
 * 用户信息模型
 */
class UserProfile extends Model
{
    protected $name = 'xiluhouse_user_profile';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'prefer_json_arr',
        'user_type_text',
        'house_state_text',
        'max_house_state_text',
        'is_broker_text',
        'tags_arr',
    ];
    public function getPreferJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['prefer_json']) ? $data['prefer_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    public function getUserTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
        $list = $this->getUserTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getMaxHouseStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['max_house_state']) ? $data['max_house_state'] : '');
        $list = $this->getHouseStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getHouseStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_state']) ? $data['house_state'] : '');
        $list = $this->getHouseStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getSourceTypeList()
    {
        return ['1' => '注册用户', '2' => '二维码用户', '3' => '线下创建用户'];
    }
    public function getUserTypeList()
    {
        return ['0' => '未询问客户', '1' => '询问客户', '2' => '一般客户', '3' => '意向客户'];
    }
    public function getHouseStateList()
    {
        return ['0' => '已流失', '1' => '已分配', '2' => '已看房', '3' => '已付订', '4' => '已签约', '5' => '已款清', '6' => '已放弃'];
    }
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function getIsBrokerTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_broker']) ? $data['is_broker'] : '');
        $list = $this->getIsBrokerList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getIsBrokerList()
    {
        return ['0' => __('Is_broker 0'), '1' => __('Is_broker 1'), '2' => __('Is_broker 2')];
    }
    public function getTagsArrAttr($value, $data)
    {
        $value = $data['tags'] ?? '';
        if ($value) {
            return explode(',', $value);
        } else {
            return [];
        }
    }
    public function broker()
    {
        return $this->belongsTo('\app\common\model\User', 'broker_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function userdata()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
